package com.gugee.ins.data.blogger.es.core;


import com.gugee.ins.common.dal.service.statistics.*;
import com.gugee.ins.common.model.es.StatisticsData;
import com.gugee.ins.common.model.es.blogger.InsBlogger;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.common.model.mysql.statistics.*;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import com.xiguaji.boot.toolkit.elasticsearch.RestClientHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class BloggerActiveFollowerDataToEsService extends EsBaseService{

    @Autowired
    InsStBloggerLanguageDao insStBloggerLanguageDao;

    @Autowired
    InsStBloggerNationDao insStBloggerNationDao;

    @Autowired
    InsStBloggerRealFollowingDao insStBloggerRealFollowingDao;

    @Autowired
    InsStBloggerRaceDao insStBloggerRaceDao;

    @Autowired
    InsStBloggerGenerationDao insStBloggerGenerationDao;

    String threadPoolName = "BloggerActiveFollowerDataToEs-Thread";

    @PostConstruct
    public void init() {
        forkJoinPool = ThreadPoolUtils.forkJoinPool(bloggerToElasticsearchAppConfig.getBloggerActiveFollowerDataToEsThreadSize(), threadPoolName);
        scheduleTemplate.attach(threadPoolName, forkJoinPool);
        workName = "BloggerActiveFollowerDataToEs";
    }

    @Override
    protected InsBlogger structureEsBlogger(InsBloggerMain insBloggerMain) {
        InsBlogger insBloggerEs = new InsBlogger();
        insBloggerEs.setId(insBloggerMain.getId());
        insBloggerEs.setAgeDistribution(insStBloggerGenerationDao.getStatisticsDataByUid(insBloggerMain.getUid()));
        insBloggerEs.setLanguageDistribution(insStBloggerLanguageDao.getStatisticsDataByUid(insBloggerMain.getUid()));
        insBloggerEs.setNationDistribution(insStBloggerNationDao.getStatisticsDataByUid(insBloggerMain.getUid()));
        insBloggerEs.setRaceDistribution(insStBloggerRaceDao.getStatisticsDataByUid(insBloggerMain.getUid()));
        insBloggerEs.setGenderDistribution(getGenderToEs(insBloggerMain.getUid()));
        insBloggerEs.setFansPortraitUpdate(System.currentTimeMillis()/1000);
        return insBloggerEs;
    }

    @Override
    protected void toEsHandler(String esIndex, List<InsBlogger> insBloggers) {
        RestClientHelper.upsert(esIndex,insBloggers);
    }

    private List<StatisticsData> getGenderToEs(long uid){
        InsStBloggerRealFollowing insStBloggerRealFollowing = insStBloggerRealFollowingDao.findByUid(uid);

        if(null == insStBloggerRealFollowing){
            return Collections.emptyList();
        }
        Double femaleRate = insStBloggerRealFollowing.getFemaleRate();

        if (null == femaleRate || 0 == femaleRate){
            return Collections.emptyList();
        }
        double maleRate = 1 - femaleRate;
        StatisticsData male = new StatisticsData();
        male.setProperty("MALE");
        male.setValue(maleRate);
        male.setRank(1);
        StatisticsData female = new StatisticsData();
        female.setProperty("FEMALE");
        female.setValue(femaleRate);
        female.setRank(1);
        List<StatisticsData> list = new ArrayList<>(2);

        if(maleRate > femaleRate){
            female.setRank(2);
            list.add(male);
            list.add(female);
        }else {
            male.setRank(2);
            list.add(female);
            list.add(male);
        }
        return list;

    }
}
